package Controle;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import modelo.Aluno;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.StreamException;

public class XMLController {

	private static String pathAluno = "data/alunos.xml";
	
	public static void saveToXML(List<Aluno> alunos) throws IOException{
		
		XStream xstream = new XStream();
		String xml = xstream.toXML(alunos);
		
		OutputStream stream = new FileOutputStream(new File(pathAluno));
		stream.write(xml.getBytes());
		stream.flush();
		stream.close();	
		
	}
	
	public static List<Aluno> readFromXML() {
		List<Aluno> alunos = null;
		XStream xstream = new XStream();
		File file = new File(pathAluno);
		try {
			alunos = (List<Aluno>) xstream.fromXML(file);
		}catch(StreamException e) {
			
			return new ArrayList<Aluno>();
		}
		return alunos;
	}
	
}
